<?php
include '../class/class.user.php';
include '../class/class.usulan.php';
include '../class/class.level.php';
include '../class/class.refcuti.php';
include '../class/class.tujuan.php';
include '../class/class.pegawai.php';
include '../class/class.kanwil.php';
include '../class/class.dokumen.php';
include '../class/class.detail.php';


if (isset($_REQUEST['aksi_rekam_usulan'])) {
    $no_surat = $_POST['no_surat'];
    $tgl_surat = $_POST['tgl_surat'];
    $perihal = $_POST['perihal'];
    $msg = false;
    $error = 'Gagal Simpan Usulan';

    session_start();
    $id_user = $_SESSION['id_user'];

    $user=new User();
    $user->id_user=$id_user;
    $userDao=new UserDao();

    $kanwil=$userDao->getUserById($user);

    $pegawaiDao= new PegawaiDao();

    $pegawai=$pegawaiDao->getPegawai($kanwil['nip']);

    $usulan = new Usulan();

    $usulan->no_surat = $no_surat;
    $usulan->tgl_surat = $tgl_surat;
    $usulan->perihal = $perihal;
    $usulan->id_user = $id_user;
    $usulan->level_usulan = 1;
    $usulan->unit=$pegawai['unit'];

    $usulanDao = new UsulanDao();

    $hasil = $usulanDao->createUsulan($usulan);

    if ($hasil) {
        $msg = true;
        $error = '';
    }

    $data = array(
        'msg' => $msg,
        'error' => $error,
    );

    echo json_encode($data);
}

if (isset($_REQUEST['list_usulan'])) {

    session_start();

    $id_user = $_SESSION['id_user'];

    $user=new User();
    $user->id_user=$id_user;

    $userDao=new UserDao();

    $usernya=$userDao->getUserById($user);

    $pegawaiDao= new PegawaiDao();

    $pegawai=$pegawaiDao->getPegawai($usernya['nip']);

    $usulanDao = new usulanDao();



    $usulan = $usulanDao->getAll($usernya['id_kewenangan'],$pegawai['unit']);
    //$kanwil = array();

    $list = array();
    $jml_peg = array();
    $data = array();
    $i = 0;
    if($usulan!=false){
        foreach ($usulan as $row) {
            $list[$i] = new usulanDao();
            //$kanwil[$i] = new KanwilDao();


            $jml = $list[$i]->getJmlPegawai($row['id_usulan']);
            if (!$jml) {
                $jml_peg[$i] = 'Belum Di Entry';
            } else {
                $jml_peg[$i] = $jml;
            }
            $label= cek_status($row['id_usulan']);
            $data[$i] = array(
                'id_usulan' => $row['id_usulan'],
                'no_surat' => $row['no_surat'],
                'tgl_surat' => $row['tgl_surat'],
                //'kanwil' => $kanwil[$i]->getNmKanwil($row['kdkanwil']),
                'jml' => $jml_peg[$i],
                'status' =>$label['status'],
                'label' =>$label['label'],
            );
            $i++;
        }
    }

    echo json_encode($data);
}

if (isset($_REQUEST['aksi_get_usulan_ubah'])) {
    $id_usulan = $_REQUEST['id_usulan'];

    $msg = false;
    $error = 'Data Kosong';

    $usulan = new Usulan();

    $usulan->id_usulan = $id_usulan;

    $usulanDao = new UsulanDao();

    $hasil = $usulanDao->getById($usulan);

    if ($hasil) {
        $msg = true;
        $error = '';
    }

    $data = array(
        'msg' => $msg,
        'error' => $error,
        'id_usulan' => $hasil['id_usulan'],
        'no_surat' => $hasil['no_surat'],
        'tgl_surat' => $hasil['tgl_surat'],
        'perihal' => $hasil['perihal'],
    );

    echo json_encode($data);
}

if (isset($_REQUEST['aksi_simpan_ubah_usulan'])) {
    $id_usulan = $_POST['id_usulan'];
    $no_surat = $_POST['no_surat'];
    $tgl_surat = $_POST['tgl_surat'];
    $perihal = $_POST['perihal'];
    $msg = false;
    $error = 'Gagal Simpan Usulan';

    session_start();
    $id_user = $_SESSION['id_user'];

    $usulan = new Usulan();
    $usulan->id_usulan = $id_usulan;
    $usulan->no_surat = $no_surat;
    $usulan->tgl_surat = $tgl_surat;
    $usulan->perihal = $perihal;
    $usulan->id_user = $id_user;
    $usulan->level_usulan = 1;

    $usulanDao = new UsulanDao();

    $hasil = $usulanDao->updateUsulan($usulan);

    if ($hasil) {
        $msg = true;
        $error = '';
    }

    $data = array(
        'msg' => $msg,
        'error' => $error,
    );

    echo json_encode($data);
}

if (isset($_REQUEST['aksi_hapus_usulan'])) {
    $id_usulan = $_REQUEST['id_usulan'];
    $usulanDao = new UsulanDao();
    $msg = false;
    $error = '';
    $jml_peg = $usulanDao->getJmlPegawai($id_usulan);

    if ($jml_peg == false) {
        $usulan_del = $usulanDao->deleteUsulan($id_usulan);
        if ($usulan_del) {
            $msg = true;
        } else {
            $error = "Gagal Hapus Usulan";
        }
    } else {
        $error = "Daftar Pegawai Masih Ada";
    }

    $data = array(
        'msg' => $msg,
        'error' => $error,
    );

    echo json_encode($data);
}

if (isset($_REQUEST['aksi_daftar_pegawai'])) {

    $id_usulan = $_REQUEST['id_usulan'];
    $msg = false;
    $daftar=array();
    
    $cuti=array();
    $tujuan=array();

    $usulanDao = new UsulanDao();
    $pegawaiDao = array(); 
    $levelDao=array();
    
    $pegawai = array();

    $list_peg = $usulanDao->getAllPegawai($id_usulan);
    if ($list_peg != false) {
        $msg = true;
        $i=0;
        foreach($list_peg as $row){
            $cuti[$i]=new RefCutiDao();
            $tujuan[$i]= new TujuanDao();
            $pegawaiDao[$i]=new PegawaiDao();
            $pegawai[$i] = $pegawaiDao[$i]->getPegawai($row['nip']);
            $levelDao[$i]= new LevelDao();
            $daftar[$i]=array(
                'id_det_usulan' => $row['id_det_usulan'],
                'id_usulan' => $row['id_usulan'],
                'nama_peg' => $pegawai[$i]['nama'],
                'nip' => $row['nip'],
                'tgl_mulai' => $row['tgl_mulai'],
                'tgl_selesai' => $row['tgl_selesai'],
                'id_cuti' => $cuti[$i]->getCuti($row['id_cuti']),
                'id_tujuan' => $tujuan[$i]->getTujuan($row['id_tujuan']),
                'level' => $levelDao[$i]->getLevel($row['id_level']),
            );
            $i++;
        }        
    }

    $data = array(
        'msg' => $msg,
        'daftar' => $daftar,
    );

    echo json_encode($data);
}

if(isset($_REQUEST['aksi_kirim_usulan'])){
    $id_usulan=$_REQUEST['id_usulan'];

    $usulanDao = new UsulanDao();
    $msg = false;
    $error = '';
    $jml_peg = $usulanDao->getJmlPegawai($id_usulan);

    if ($jml_peg != false) {
        $dokumenDao = new DokumenDao();
        $isDokumen=$dokumenDao->getListDokumen($id_usulan);
        if($isDokumen!=false){
            $kirim=$usulanDao->kirimUsulan($id_usulan);
            if($kirim){
                $msg=true;
            }else{
                $error="Gagal Kirim Ke Bag. Kepeg";
            }
        }else{
            $error="Dokumen Belum Diupload";
        }

    } else {
        $error = "Pegawai Belum Dientri";
    }

    $data = array(
        'msg' => $msg,
        'error' => $error,
    );

    echo json_encode($data);


}
function cek_status($id_usulan){

    $usulanDao = new UsulanDao();
    $jml_peg = $usulanDao->getJmlPegawai($id_usulan);
    $dokumenDao = new DokumenDao();
    $isDokumen=$dokumenDao->getListDokumen($id_usulan);
    $status='';
    $label='';
    if($jml_peg==false || $isDokumen==false){
        $label= 'warning';
        $status= 'Belum Lengkap';
  
    }else{
        $detailDao=new DetailDao();
        $adaTolak=$detailDao->cekDetail($id_usulan,2);
        if($adaTolak==false){
            $adaKonfirmasi=$detailDao->cekDetail($id_usulan,3);
            if($adaKonfirmasi==false){
                $diterima=$detailDao->cekDetail($id_usulan,4);
                if($diterima==false){
                    $disetujui=$detailDao->cekDetail($id_usulan,6);
                    if($disetujui==false){
                        $dikirim=$detailDao->cekDetail($id_usulan,7);
                        if($dikirim!=false){
                            $label= 'info';
                            $status= 'Terkirim Ke Kepeg';
                        }else{
                            $label= 'info';
                            $status= 'Lengkap, Siap Dikirim';
                        }
                    }else{
                        $label= 'success';
                        $status= 'Disetujui Sekjen';
                    }
                }else{
                    $label= 'primary';
                    $status= 'Diterima Kepegawaian';
                }
            }else{
                $label= 'info';
                $status= 'Perlu Konfirmasi';
            }
        }else{
            $label= 'danger';
            $status= 'Ditolak';
        }
    }

    $data=array(
        'label' => $label,
        'status' => $status,
        );
    return $data;
}
?>